Skip to content

Do not reference pthread_atfork in non-SMP_SERVER mode - #345

Merged
xianyi merged 2 commits into
OpenMathLib:developfrom
ogrisel:fix-non-smp-server-pthread_atfork-reference
Feb 25, 2014
Merged

xianyi merged 2 commits into
OpenMathLib:developfrom
ogrisel:fix-non-smp-server-pthread_atfork-reference

Conversation

@ogrisel

@ogrisel ogrisel commented Feb 20, 2014

Copy link
Copy Markdown
Contributor

Tentative fix for:

#294 (comment)

@cartazio

Copy link
Copy Markdown

So I tested this out just now.

  1. even without this patch, IF I also instruct my linker to link libpthreads for my ffi binding, things work.
  2. i just tried with your patch https://travis-ci.org/wellposed/hOpenBLAS/jobs/19282530#L251 (notice the checkout line at the top when you click the link) , same problem.

I tried to reproduce the issue on my mac, but i may have been building it locally using clang by accident rather than gcc.

anyways, the original problem is fixed by having my ffi binding (when applicable) explicitly link in libpthreads, though I don't know if thats a good surprise to throw at folks down stream

@ogrisel

ogrisel commented Feb 20, 2014

Copy link
Copy Markdown
Contributor Author

What I don't understand is that if the library is built with the threading mode (e.g. USE_OPENMP=0) linking against pthread should be required anyway and with USE_OPENMP=1 the fix in this PR should drop the code section that references to the pthread_atfork hence this fix should solve the link issue you observe.

@xianyi any idea why this is happening?

@cartazio

Copy link
Copy Markdown

hrm, i'm building it with the vanilla settings in my TravisCI (not explicitly setting any of the build flags, I think)

@xianyi

xianyi commented Feb 20, 2014

Copy link
Copy Markdown
Collaborator

For USE_OPENMP=1, OpenBLAS also defined SMP_SERVER.

Please use ifndef USE_OPENMP instead of detecting SMP_SERVER.

@cartazio

Copy link
Copy Markdown

and USE_OPEMP is set to 0 by default when built with gcc?

@xianyi

xianyi commented Feb 21, 2014

Copy link
Copy Markdown
Collaborator

Yes, it doesn't set USE_OPENMP by default.
2014-2-21 ÉÏÎç7:28ÓÚ "Carter Tazio Schonwald" notifications@github.comдµÀ£º

and USE_OPEMP is set to 0 by default when built with gcc?

¡ª
Reply to this email directly or view it on GitHubhttps://github.com//pull/345#issuecomment-35682351
.

@cartazio

Copy link
Copy Markdown

ok, so its not all my fault :)

@ogrisel

ogrisel commented Feb 21, 2014

Copy link
Copy Markdown
Contributor Author

Alright but if OpenBLAS uses pthread by default (as was presumably also the case in past versions of OpenBLAS), I don't understand how the new use of pthread_atfork would change the requirements to link with pthread or not for a third party program that links with OpenBLAS.

Update: I googled a bit more and apparently the GLIBC has a special handling of the export of the pthread_atfork symbol: http://ryanarn.blogspot.fr/2011/07/curious-case-of-pthreadatfork-on.html

The article is only about PPC but some of those considerations might also apply for other architectures. Apparently the link order is very important for this symbol and the author presents several possible options at the end of the article. In particular appending -Wl,-u,pthread_atfork to the link options when generating the final libopenblas.so might solve the problem reported by @cartazio.

@ogrisel

ogrisel commented Feb 21, 2014

Copy link
Copy Markdown
Contributor Author

Weird, I cannot reproduce the issue when building this sample program:

https://gist.github.com/ogrisel/6440446#file-openblas_fork-c

I do not need to link -lpthread explicitly to build my custom program with gcc-4.7 (under OSX): just linking against -lopenblas is enough. Furthermore I checked the Makefiles of OpenBLAS and from what I can see -lpthread is always a the end of the line...

@ogrisel

ogrisel commented Feb 21, 2014

Copy link
Copy Markdown
Contributor Author

Actually I made a mistake, I think I built openblas with clang rather than GCC, rebuilding from scratch now.

@ogrisel

ogrisel commented Feb 21, 2014

Copy link
Copy Markdown
Contributor Author

Even re-building with GCC I still cannot reproduce the problem. However I cannot find any single occurrence of pthread in the OpenBLAS build log... I should probably try from scratch under Linux but don't have access to my linux box right now.

@ogrisel

ogrisel commented Feb 24, 2014

Copy link
Copy Markdown
Contributor Author

I tried again on a Linux with libc 2.15-0ubuntu10.5 and gcc 4.6.3-1ubuntu5 and I do not need to link explicitly against pthread when building a program that links against a freshly built libopenblas.so.

@cartazio could you please try to provide me with a minimalistic reproduction case? Preferably written in C?

@cartazio

Copy link
Copy Markdown

@ogrisel i've a few work / business / income related deadlines this week, so no promises about being able to get to it this week

Have you written any code that tickles the setthreads function in openblas? "openblas_set_num_threads"
Thats what I"m tickling in my code.

@cartazio

Copy link
Copy Markdown

also, in my case I'm static linking to openblas.

@cartazio

Copy link
Copy Markdown

if no progress happesn this week, i can def make the time to try to cook up a minimal repro next week

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

I'll check static linking and openblas_set_num_threads.

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

@cartazio I confirm static linking is the trigger:
Here is the test program I used:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <cblas.h>


void* xmalloc(size_t n)
{
    void* tmp;

    tmp = malloc(n);
    if (tmp == NULL) {
        fprintf(stderr, "You are about to die\n");
        exit(1);
    } else {
        return tmp;
    }
}


int main(int argc, char *argv[])
{
    int n;
    int i;

    double *a, *b, *c;
    size_t n_bytes;

    if (argc != 2) {
        fprintf(stderr, "USAGE: %s size\n", argv[0]);
        exit(1);
    } else {
        n = atoi(argv[1]);
        printf("computing for size %d\n", n);
    }

    n_bytes = sizeof(*a) * n * n;

    a = xmalloc(n_bytes);
    b = xmalloc(n_bytes);
    c = xmalloc(n_bytes);

    for(i = 0; i < n * n; ++i) {
        a[i] = 1;
        b[i] = 1;
    }
    printf("calling DGEMM...\n");
    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n,
                1.0, a, n, b, n, 0.0, c, n);
    printf("done.\n");
    return 0;
}

When I build with a static link to openblas and without pthread, I get:

$ gcc -static -I/opt/OpenBLAS/include -L/opt/OpenBLAS/lib  -o /tmp/openblas_custom_program ~/tmp/openblas_custom_program.c -lopenblas
/opt/OpenBLAS/lib/libopenblas.a(memory.o): In function `openblas_fork_handler':
memory.c:(.text+0x290): undefined reference to `pthread_atfork'
...

while it works if I build with an additional pthread link on the custom program:

gcc -static -I/opt/OpenBLAS/include -L/opt/OpenBLAS/lib  -o /tmp/openblas_custom_program ~/tmp/openblas_custom_program.c -lopenblas -lpthread

@cartazio

Copy link
Copy Markdown

Ok. So is this a bug in OpenBLAS' static linking, or do I need to handle that on my side?

@cartazio

Copy link
Copy Markdown

also thank you very very very much @ogrisel, I appreciate you taking the time to help out on this.
(also why was this bug not happening to me before your patch? Mind you I've not done a proper bisect on the openblas side due to my wee computer)

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

Ok. So is this a bug in OpenBLAS' static linking, or do I need to handle that on my side?

@cartazio I don't know. I am not a pthread / glibc expert.

(also why was this bug not happening to me before your patch?)

The pthread_at_fork symbol seems to be special somehow. Many other pthread symbols are referenced in memory.o and blas_server.o and they are linked implicitly for some reason:

$ nm driver/others/blas_server.o | grep pthread
0000000000000910 T gotoblas_pthread
                 U pthread_cond_destroy
                 U pthread_cond_init
                 U pthread_cond_signal
                 U pthread_cond_wait
                 U pthread_create
                 U pthread_join
                 U pthread_mutex_destroy
                 U pthread_mutex_init
                 U pthread_mutex_lock
                 U pthread_mutex_unlock
$ nm driver/others/memory.o | grep pthread
                 U pthread_atfork
                 U pthread_mutex_lock
                 U pthread_mutex_unlock

We could try to extract the pthread_atfork object from /usr/lib/libpthread.a with:

$ ar -x /usr/lib/libpthread.a pthread_atfork.o

and then include pthread_atfork.o in the libopenblas.a archive but that sounds like a hack to me. Maybe we should just update the README.md of OpenBLAS to state that statically linking against libopenblas.a might require to also use the -lpthread flag.

Any opinion @njsmith & @xianyi?

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

@xianyi independently of the pthread_atfork linking issue I think you should merge this PR as:

  • the ifdef SMP_SERVER check is more consistent
  • I just fixed a compile warning.

@njsmith

njsmith commented Feb 25, 2014

Copy link
Copy Markdown

pthread_atfork is indeed quite special, see:
http://caml.inria.fr/mantis/view.php?id=5636
http://ryanarn.blogspot.co.uk/2011/07/curious-case-of-pthreadatfork-on.html?m=1

In particular, on glibc systems, doing -lpthread actually does statically
link pthread_atfork always, so it's hard for me to imagine that the
suggested static linking hack could be useful.

Can you build the static library with -pthread or -lpthread?
On 25 Feb 2014 04:49, "Olivier Grisel" notifications@github.com wrote:

Ok. So is this a bug in OpenBLAS' static linking, or do I need to handle
that on my side?

@cartazio https://github.com/cartazio I don't know. I am not a pthread
/ glibc expert.

(also why was this bug not happening to me before your patch?)

The pthread_at_fork symbol seems to be special somehow. Many other
pthread symbols are referenced in memory.o and blas_server.o and they are
linked implicitly for some reason:

$ nm driver/others/blas_server.o | grep pthread
0000000000000910 T gotoblas_pthread
U pthread_cond_destroy
U pthread_cond_init
U pthread_cond_signal
U pthread_cond_wait
U pthread_create
U pthread_join
U pthread_mutex_destroy
U pthread_mutex_init
U pthread_mutex_lock
U pthread_mutex_unlock
$ nm driver/others/memory.o | grep pthread
U pthread_atfork
U pthread_mutex_lock
U pthread_mutex_unlock

We could try to extract the pthread_atfork object from
/usr/lib/libpthread.a with:

$ ar -x /usr/lib/libpthread.a pthread_atfork.o

and then include pthread_atfork.o in the libopenblas.a archive but that
sounds like a hack to me. Maybe we should just update the README.md of
OpenBLAS to state that statically linking against libopenblas.a might
require to also use the -lpthread flag.

Any opinion @njsmith https://github.com/njsmith & @xianyihttps://github.com/xianyi
?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/345#issuecomment-35991099
.

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

Thanks @njsmith for the help.

Can you build the static library with -pthread or -lpthread?

If I am not mistaken, the static library is built by assembling object files into an archive manually with ar:

https://github.com/xianyi/OpenBLAS/blob/develop/interface/Makefile#L420

I have tried to manually add a -pthread or -lpthread to the compile line of memory.c and blas_server.c:

https://github.com/xianyi/OpenBLAS/blob/develop/driver/others/Makefile#L97
https://github.com/xianyi/OpenBLAS/blob/develop/driver/others/Makefile#L101

but that does not solve the issue. Maybe it should be added to all the objects files that need threads such as:

https://github.com/xianyi/OpenBLAS/blob/develop/interface/Makefile#L1126

But that sounds cumbersome to me.

@xianyi

xianyi commented Feb 25, 2014

Copy link
Copy Markdown
Collaborator

@ogrisel , I used your test codes and static link the library on my Linux box. I got lots of undefined error.

./OpenBLAS//libopenblas.a(memory.o): In function `_touch_memory':
memory.c:(.text+0x15): undefined reference to `pthread_mutex_lock'
memory.c:(.text+0x41): undefined reference to `pthread_mutex_unlock'
./OpenBLAS//libopenblas.a(memory.o): In function `openblas_fork_handler':
memory.c:(.text+0x440): undefined reference to `pthread_atfork'
./OpenBLAS//libopenblas.a(memory.o): In function `blas_memory_alloc':
memory.c:(.text+0x7a5): undefined reference to `pthread_mutex_lock'
memory.c:(.text+0x825): undefined reference to `pthread_mutex_unlock'
./OpenBLAS//libopenblas.a(memory.o): In function `blas_shutdown':
memory.c:(.text+0x9e1): undefined reference to `pthread_mutex_lock'
memory.c:(.text+0xa6e): undefined reference to `pthread_mutex_unlock'
./OpenBLAS//libopenblas.a(blas_server.o): In function `blas_thread_server':
blas_server.c:(.text+0x273): undefined reference to `pthread_mutex_lock'
blas_server.c:(.text+0x287): undefined reference to `pthread_mutex_unlock'
blas_server.c:(.text+0x33f): undefined reference to `pthread_cond_wait'
./OpenBLAS//libopenblas.a(blas_server.o): In function `blas_thread_init':
blas_server.c:(.text+0x416): undefined reference to `pthread_mutex_lock'
blas_server.c:(.text+0x4be): undefined reference to `pthread_mutex_init'
blas_server.c:(.text+0x4ca): undefined reference to `pthread_cond_init'
blas_server.c:(.text+0x4e0): undefined reference to `pthread_create'
blas_server.c:(.text+0x50f): undefined reference to `pthread_mutex_unlock'
...

Thus, I think the user should add -lpthread for static link. We can put the error to the FAQ document.

xianyi added a commit that referenced this pull request Feb 25, 2014
…k-reference

Do not reference pthread_atfork in non-SMP_SERVER mode
@xianyi
xianyi merged commit 322a178 into OpenMathLib:develop Feb 25, 2014
@cartazio

Copy link
Copy Markdown

thanks!

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

@njsmith this SO answer states that pthread being part of libc one should not statically link against it unless one links to the whole libc as well. So it's probably a bad idea to include pthread as part of the libopenblas.a archive.

I will edit the FAQ to add an entry on that.

@ogrisel

ogrisel commented Feb 25, 2014

Copy link
Copy Markdown
Contributor Author

@cartazio

Copy link
Copy Markdown

ok, so if i dylink to openblas i won't have this problem? cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants